//  验证登录, 如果没有登录过, 那么不会展示该页面
const token = window.localStorage.getItem('token')
const goodsId = window.localStorage.getItem('goodsId')

getInfo()

// 获取商品信息
function getInfo() {
  // 发送请求请求商品信息
  $.ajax({
    url: 'http://localhost:8888/goods/admin_info',
    method: 'GET',
    data: { goodsId: goodsId },
    headers: { authorization: token },
    success (res) {
      console.log(res.info)
        $('form [name=campuls]').val(res.info.campuls)
        $('form [name=category]').val(res.info.category)
        $('form [name=quality]').val(res.info.quality_type)
        $('form [name=price]').val(res.info.price)
        $('form [name=email]').val(res.info.email)
        $('form [name=phone]').val(res.info.phone)
        $('form [name=img]').val(res.info.goods_img)
        $('form [name=title]').val(res.info.title)
    }
  })
}

// 修改商品信息
$('form').on('submit', function (e) {
  // 阻止默认行为
  e.preventDefault()

  // 采集商品信息
  const data = $('form').serialize()

  $.ajax({
    url: 'http://localhost:8888/goods/admin_update',
    method: 'POST',
    data: data+"&goodsId="+goodsId,
    headers: { authorization: token },
    success (res) {
      if(res.code===2){
        window.alert('修改信息成功返回商品页面')
        window.location.href = './goods.html'
      }
    }
  })
})
